package com.splash;

import com.essential.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		handler = new Handler() {
			private int count = 0;

			public void handleMessage(Message msg) {
				imageView.setImageResource(slide[count++]);
				imageView.invalidate();
				if (count >= slide.length) {
					count = 0;
				}
			}
		};

		Thread splashTimer = new Thread() {
			public void run() {
				try {
					for (int i = 0; i < slide.length; i++) {
						long ms = 0;
						while (splashActive && ms < slideInterval) {
							sleep(100);
							if (!paused) {
								ms += 100;
							}
						}
						handler.sendMessage(handler.obtainMessage());
					}
					sleep(slideInterval);
					startActivity(new Intent("com.splash.MainActivity"));
				} catch (Exception e) {
					Log.e(getErrTag(), e.toString());
				} finally {
					finish();
				}
			}
		};

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.splash);
		/**
		 * findViewById(R.id.res_layout_splash_imageview).setSystemUiVisibility(
		 * View.STATUS_BAR_HIDDEN);
		 */
		findViewById(R.id.res_layout_splash_imageview).setVisibility(
				View.VISIBLE);

		imageView = (ImageView) this
				.findViewById(R.id.res_layout_splash_imageview);
		splashTimer.start();

	}

	private static final int[] slide = { R.drawable.flower, R.drawable.donga,
			R.drawable.mountainvillage };
	private Handler handler;
	private ImageView imageView;
	private boolean splashActive = true;
	private boolean paused = false;
	private long slideInterval = 1500; // millisecond

	private final String getErrTag() {
		return this.getClass().getName();
	}

}
